package edu.iss.javaproject.controller;

import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import edu.iss.javaproject.exception.LeaveNotFoundException;
import edu.iss.javaproject.exception.UserNotFoundException;
import edu.iss.javaproject.model.LeaveRecord;
import edu.iss.javaproject.model.Users;
import edu.iss.javaproject.service.LAPService;
import edu.iss.javaproject.util.ConvertDate;

@Controller
@SessionAttributes("sessionuser")
@RequestMapping(value = "/staffHome")
public class StaffController {
	
	@Autowired
	private LAPService lapService;

	private static final Logger logger = LoggerFactory.getLogger(StaffController.class);
	
	@RequestMapping(value = "", method = RequestMethod.GET)
	public String home(Locale locale, Model model, @ModelAttribute("sessionuser") Users user ) {
		logger.info("Welcome home! The client locale is {}.", user.toString());
		
		Date date = new Date();
		DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);
		
		String formattedDate = dateFormat.format(date);
		
		model.addAttribute("serverTime", formattedDate );
		
		return "staffHome";
	}
	@RequestMapping(value = "/movementRegister", method = RequestMethod.GET)
	public ModelAndView movementRegister(@RequestParam(value="month", required=false, defaultValue ="1") String date,@ModelAttribute("sessionuser") Users user) throws UserNotFoundException, LeaveNotFoundException {
		logger.info("Welcome home! The client locale is {}.",user.toString());
		HashMap<Integer,Users> map = new HashMap<Integer,Users>();
		ModelAndView mv = new ModelAndView("movementRegister");
		List<LeaveRecord> list = lapService.findAllLeaveRecords();
		List<LeaveRecord> listRecord = new ArrayList<LeaveRecord>();
		ConvertDate cd = new ConvertDate();
		Date start;
		Date end;
		int realDate = Integer.valueOf(date)-1;
		logger.info("Month is "+ realDate);
		for(int i=0;i<list.size();i++)
		{
			start= list.get(i).getLeaveStart();
			end = list.get(i).getLeaveEnd();
			logger.info("Month is "+cd.GetMoth(start));
			if(cd.GetMoth(start)==realDate|| cd.GetMoth(end)==realDate)
			{
				
				listRecord.add(list.get(i));
				map.put(i, lapService.findByID(list.get(i).getId()));
			}
		}
		mv.addObject("staffRecord", map );
		mv.addObject("leaveRecord", listRecord);
		
		return mv;
	}
	@RequestMapping(value="/leavelist",method=RequestMethod.GET)
	public String setleave(@ModelAttribute ("sessionuser")Users user,  BindingResult result,Map<String, Object> map){
		LeaveRecord Record=new LeaveRecord();
		map.put("Record", Record);
		try
		{
	Users userS =user;
		map.put("leavelist", lapService.findByUserID(userS.getId()));
		}
		catch(LeaveNotFoundException e)
		{
			
		}
		return "leavelist";
	}	
	@RequestMapping(value="/leavelist/{id}",method=RequestMethod.GET)
	public String setleave(@PathVariable("id") int Id ,Map<String, Object> map){
		LeaveRecord Record=new LeaveRecord();
		map.put("Record", Record);
		
		try
		{
		map.put("leavelist", lapService.findByUserID(Id));
		}
		catch(LeaveNotFoundException e)
		{
			
		}
		return "leavelist";
	}	
	@RequestMapping(value="/details", method=RequestMethod.POST)
	public String doActions(@ModelAttribute LeaveRecord Record, BindingResult result, @RequestParam String action, Map<String, Object> map)
	{
		map.put("leaveRecord", Record);
		return "leaveUpdate";
	}

}
